package com.lmscn.lms.domain.exam;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.lmscn.lms.api.util.JsonUtil;

import net.sf.ezmorph.bean.MorphDynaBean;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;

public class ExamQuestionArea implements java.io.Serializable{
	private Long examId;
	private Map<Long,ExamQuestionRule> examRules;
	public Long getExamId() {
		return examId;
	}
	public void setExamId(Long examId) {
		this.examId = examId;
	}
	public Map<Long,ExamQuestionRule> getExamRules() {
		return examRules;
	}
	public void setExamRules(Map<Long,ExamQuestionRule> examRules) {
		this.examRules = examRules;
	}
	
	public void addExamRole(Long categoryId,ExamQuestionRule examRule){
		if(this.examRules == null){
			this.examRules = new HashMap<Long,ExamQuestionRule>();
		}
		this.examRules.put(categoryId, examRule);
	}
	
	/**
	 * @Author:ZhangHaiwei
	 * @Description:解析字符串获取抽题规则
	 * @param str
	 * @return
	 */
	public static Map<Long,ExamQuestionRule> getExamQuestionRuleByStr(String str){
		if(str == null || str.trim().length() == 0){
			return null;
		}
		//return JSONObject.toBean(str, Map.class);
		Map<String,MorphDynaBean> map = JsonUtil.jsonToObject(str, Map.class);
		Map<Long,ExamQuestionRule> rules= new HashMap<Long,ExamQuestionRule>();
		for(Entry<String, MorphDynaBean> entry:map.entrySet()){
			MorphDynaBean bean = entry.getValue();
			ExamQuestionRule rule = new ExamQuestionRule();
			rule.setDetermineChooseNum(Integer.parseInt(bean.get("determineChooseNum").toString()));
			rule.setFillBlankNum(Integer.parseInt(bean.get("fillBlankNum").toString()));
			rule.setMultiChoiceNum(Integer.parseInt(bean.get("multiChoiceNum").toString()));
			rule.setSingleChoiceNum(Integer.parseInt(bean.get("singleChoiceNum").toString()));
			rules.put(Long.parseLong(entry.getKey().toString()), rule);
		}
		return rules;
	}
	
	/**
	 * @Author:ZhangHaiwei
	 * @Description:将抽题规则转为xml
	 * @param examRules
	 * @return
	 */
	public static String getStrFromExamQuestionRule(Map<Long,ExamQuestionRule> examRules){
		if(examRules == null || examRules.size() <= 0){
			return null;
		}
		Map<String,ExamQuestionRule> map = new HashMap<String,ExamQuestionRule>();
		for(Entry<Long,ExamQuestionRule> entry:examRules.entrySet()){
			map.put(entry.getKey().toString(), entry.getValue());
		}
//		JsonConfig jsonConfig = new JsonConfig();
//	    jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
//	    jsonConfig.setIgnoreDefaultExcludes(true);
//	    jsonConfig.setAllowNonStringKeys(true);
//	    return JSONObject.fromObject(examRules, jsonConfig).toString();
		return JsonUtil.objectToJson(map);
	}
	
	public static void main(String[] args) {
		Map<Long,ExamQuestionRule> examRules = new HashMap<Long,ExamQuestionRule>();
		ExamQuestionRule rule = new ExamQuestionRule();
		rule.setSingleChoiceNum(25);
		rule.setMultiChoiceNum(15);
		examRules.put(2L, rule);
		//String json = JSONObject.fromObject(examRules).toString();
		String json = ExamQuestionArea.getStrFromExamQuestionRule(examRules);
		//System.out.println(json);
		Map<?,?> map = ExamQuestionArea.getExamQuestionRuleByStr(json);
		//System.out.println(((MorphDynaBean)map.get("2")).get("determineChooseNum"));
		//System.out.println(map);
	}
	
	
	
}
